//index.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')

Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    swiperCurrent: 0,
    selectCurrent: 0,
    activeCategoryId: 0,
    userInfo: {},
    topics: [],
    categories: [],
    banners: [],
    goods: [],
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    this.getBanners()
    this.getCategories()
    this.getGoods(this.data.activeCategoryId)
  },
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getGoods(this.data.activeCategoryId);
  },

  getBanners: function () {
    const that = this
    wx.request({
      url: config.service.requestApiUrl + '/banners',
      success: function (res) {
        if (res.data.code == 404) {
          wx.showModal({
            title: '提示',
            content: '请在后台添加 banner 轮播图片',
            showCancel: false
          })
        } else {
          that.setData({
            banners: res.data
          });
        }
      }
    })
  },
  getCategories: function () {
    const that = this
    wx.request({
      url: config.service.requestApiUrl + '/shop/goods/categories',
      success: function (res) {
        if (res.data.code == 404) {
          wx.showModal({
            title: '提示',
            content: '请在后台添加 banner 轮播图片',
            showCancel: false
          })
        } else {
          that.setData({
            categories: [{ id: 0, name: "全部" }] .concat(res.data)
          });
        }
      }
    })
  },
  getGoods: function (activeCategoryId) {
    const that = this
    wx.request({
      url: config.service.requestApiUrl + '/shop/goods',
      data:{
        categoryId: activeCategoryId,
      },
      success: function (res) {
        if (res.data.code == 404) {
          wx.showModal({
            title: '提示',
            content: '请在后台添加 banner 轮播图片',
            showCancel: false
          })
        } else {
          that.setData({
            goods: res.data
          });
        }
      }
    })
  },

  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/topic/topic?id=" + e.currentTarget.dataset.id
    })
  },
})
